package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsModelProduct;
import net.realsee.domain.wcs.data.bo.WcsModelProductBo;
import net.realsee.domain.wcs.data.vo.WcsModelProductVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsModelProductMapper;
import net.realsee.service.wcs.data.IWcsModelProductService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 机种物料关系机种和物料的对应关系(一个机种包含多个物料)Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsModelProductServiceImpl extends ServiceImpl<WcsModelProductMapper, WcsModelProduct> implements IWcsModelProductService {

    private final WcsModelProductMapper baseMapper;

    /**
     * 查询机种物料关系机种和物料的对应关系(一个机种包含多个物料)
     */
    @Override
    public WcsModelProductVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询机种物料关系机种和物料的对应关系(一个机种包含多个物料)列表
     */
    @Override
    public TableDataInfo<WcsModelProductVo> selectVoPage(WcsModelProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsModelProduct> lqw = buildVoQueryWrapper(bo);
        Page<WcsModelProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询机种物料关系机种和物料的对应关系(一个机种包含多个物料)列表
     */
    @Override
    public List<WcsModelProductVo> selectVoList(WcsModelProductBo bo) {
        LambdaQueryWrapper<WcsModelProduct> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsModelProduct> buildVoQueryWrapper(WcsModelProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsModelProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsModelProduct::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getModelCode()), WcsModelProduct::getModelCode, bo.getModelCode());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNo()), WcsModelProduct::getProductNo, bo.getProductNo());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsModelProduct::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsModelProduct::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsModelProduct::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsModelProduct::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsModelProduct::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsModelProduct::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsModelProduct::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsModelProduct::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsModelProduct::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsModelProduct::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsModelProduct::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsModelProduct::getRowVersionStamp, bo.getRowVersionStamp());
        return lqw;
    }

    /**
     * 新增机种物料关系机种和物料的对应关系(一个机种包含多个物料)
     */
    @Override
    public Boolean insertByBo(WcsModelProductBo bo) {
        WcsModelProduct add = BeanUtil.toBean(bo, WcsModelProduct.class);
        return this.save(add);
    }

    /**
     * 修改机种物料关系机种和物料的对应关系(一个机种包含多个物料)
     */
    @Override
    public Boolean updateByBo(WcsModelProductBo bo) {
        WcsModelProduct update = BeanUtil.toBean(bo, WcsModelProduct.class);
        return this.updateById(update);
    }

    /**
     * 批量删除机种物料关系机种和物料的对应关系(一个机种包含多个物料)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
